package practise.alogrithm;

import java.util.Arrays;

/**
 * Created by Richard on 7/28/2014.
 */
public class OrderMaker {
    private static boolean place(int n, int[] arr) {
        if (n == 0) return true;
        for (int i = 0; i + n + 1 < arr.length; ++i) {
            if (arr[i] == 0 && arr[i + n + 1] == 0) {
                arr[i] = arr[i + n + 1] = n;
                if (place(n - 1, arr)) return true;
                arr[i] = arr[i + n + 1] = 0;
            }
        }
        return false;
    }

    public static int[] makeNumberDistanceEqualsToNumber(int length) {
        int[] arr = new int[length];
        Arrays.fill(arr, 0);
        if (place(length >> 1, arr)) return arr;
        return null;
    }

    public static void main(String args[]) {
        for(int i=1; i<30;i++) {
            System.out.println("index:"+i);
            int[] arr = makeNumberDistanceEqualsToNumber(i);
            if (arr != null) System.out.println(Arrays.toString(arr));
            else System.out.println("Can not make such array");
        }
    }

    private static void createAndDisplayArray(int input) {

        int size = 2*input;
        int arr[] = new int[size];

        //This will be the first element to be entered. You can use a bounded random no generator to get this.
        int seed = 2;

        for(int i =0;i<size; i++){
            if(arr[i] <= 0){
                if(seed > input){
                    seed = seed/input;
                }
                arr[i] = seed;
                int seedPosition = arr[i] + i + 1;
                if(seedPosition < size && arr[seedPosition] <= 0){
                    arr[seedPosition] = seed;
                }
                ++seed;
            }

        }
        for(int i = 0 ; i < size;i++){
            System.out.print(" " + arr[i]);
        }
    }
}
